package com.lyf.shop.interceptor;

import com.lyf.shop.constant.LoginConstant;
import com.lyf.shop.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录判断拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(LoginConstant.LOGIN_USER_KEY);
        if (user==null){
            response.sendRedirect("/user/toLogin");
        }
        return true;
    }
}
